package org.finesys.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.finesys.system.api.dto.AppDto;
import org.finesys.system.api.entity.SysApp;
import org.finesys.system.convert.SysAppConvert;
import org.finesys.system.mapper.SysAppMapper;
import org.finesys.system.service.SysAppService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysAppServiceImpl extends BaseServiceImpl<SysAppMapper, SysApp> implements SysAppService {
    @Override
    public Boolean saveApp(AppDto appDto) {
        SysApp sysApp = SysAppConvert.INSTANCE.convert(appDto);
        return baseMapper.insert(sysApp) > 0;
    }

    @Override
    public Boolean updateApp(AppDto appDto) {
        SysApp sysApp = SysAppConvert.INSTANCE.convert(appDto);
        return baseMapper.updateById(sysApp) > 0;
    }

    @Override
    public Boolean deleteApp(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public AppDto findById(Long id) {
        SysApp sysApp = baseMapper.selectById(id);
        AppDto appDto = SysAppConvert.INSTANCE.convert(sysApp);
        return appDto;
    }

    /**
     * 获取全部，并排序
     */
    @Override
    public List<AppDto> listApp() {
        LambdaQueryWrapper<SysApp> lambdaQueryWrapper = Wrappers.<SysApp>lambdaQuery().orderByAsc(SysApp::getSortOrder);
        return SysAppConvert.INSTANCE.convertList(baseMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public IPage<AppDto> pageApp(Page<AppDto> page, AppDto appDto) {
        Page<SysApp> sysAppPage = new Page<>();
        sysAppPage.setCurrent(page.getCurrent());
        sysAppPage.setSize(page.getSize());
        SysApp sysApp = SysAppConvert.INSTANCE.convert(appDto);
        Page<SysApp> appPage = findPage(sysAppPage, sysApp);
        return SysAppConvert.INSTANCE.convertPage(appPage);
    }
}
